package com.sunday.aomi.ui.mine.statistic;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.MD5Utils;
import com.sunday.common.utils.ToastUtils;
import com.sunday.aomi.R;
import com.sunday.aomi.entity.ProfitStatistic;
import com.sunday.aomi.http.ApiClient;
import com.sunday.member.utils.SignUtil;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by 刘涛 on 2017/6/12.
 */

public class ProfitStatisticActivity extends BaseActivity {


    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.total_profit)
    TextView totalProfit;
    @Bind(R.id.sell_order_num)
    TextView sellOrderNum;
    @Bind(R.id.layout_order_num)
    LinearLayout layoutOrderNum;
    @Bind(R.id.sell_total_money)
    TextView sellTotalMoney;
    @Bind(R.id.layout_total_money)
    LinearLayout layoutTotalMoney;
    @Bind(R.id.buy_order_money)
    TextView buyOrderMoney;
    @Bind(R.id.layout_buy_order_num)
    LinearLayout layoutBuyOrderNum;
    @Bind(R.id.buy_total_money)
    TextView buyTotalMoney;
    @Bind(R.id.layout_buy_total_money)
    LinearLayout layoutBuyTotalMoney;
    @Bind(R.id.order_num)
    TextView orderNum;

    private ProfitStatistic statistic;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_statistics);
        ButterKnife.bind(this);
        title.setText("营收统计");
        getData();
    }

    @SuppressLint("DefaultLocale")
    private void updateView(ProfitStatistic statistic){
        sellOrderNum.setText(String.format("%d", statistic.getXsdd()));
        sellTotalMoney.setText(statistic.getXsze());
        buyOrderMoney.setText(statistic.getCgddsfje());
        buyTotalMoney.setText(statistic.getCgddyfje());
        totalProfit.setText(statistic.getRevenue());
        orderNum.setVisibility(statistic.getCdddsl()==0? View.GONE:View.VISIBLE);
        orderNum.setText(String.format("（%d单）",statistic.getCdddsl()));
    }

    private void getData(){
        String nonce= MD5Utils.random();
        String[] str1=new String[]{"type","nonce"};
        String[] str2=new String[]{"2",nonce};
        Call<ResultDO<ProfitStatistic>> call= ApiClient.getApiAdapter().getRevenue(2,nonce, SignUtil.getSign(str1,str2));
        call.enqueue(new Callback<ResultDO<ProfitStatistic>>() {
            @Override
            public void onResponse(Call<ResultDO<ProfitStatistic>> call, Response<ResultDO<ProfitStatistic>> response) {
                if (isFinish||response.body()==null){return;}
                if (response.body().getCode()==0){
                    updateView(response.body().getResult());
                    statistic=response.body().getResult();
                }else {
                    ToastUtils.showToast(mContext,response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO<ProfitStatistic>> call, Throwable t) {
                ToastUtils.showToast(mContext,R.string.network_error);
            }
        });

    }

    @OnClick({R.id.layout_buy_total_money,R.id.layout_total_money})
    void onClick(View v){
        switch (v.getId()){
            case R.id.layout_buy_total_money:
                intent=new Intent(mContext,TotalProfitListActivity.class);
                intent.putExtra("type",1);
                startActivity(intent);
                break;
            case R.id.layout_total_money:
                intent=new Intent(mContext,TotalProfitListActivity.class);
                intent.putExtra("type",2);
                startActivity(intent);
                break;
        }
    }



}
